home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 1995 #5 & #6 / Amiga Plus CD - 1995 - No. 5 and 6.iso / pd / grafik / raystorm / examples / sphanim.cpp < prev    next >
C/C++ Source or Header  |  1995-08-10  |  8KB  |  347 lines

  1. /***************
  2.  * NAME:          sphanim.cpp
  3.  * VERSION:       1.0 10.08.1995
  4.  * DESCRIPTION:   Little anim of jumping balls
  5.  *                        this program has been compiled with Maxon C++ 3.0
  6.  *    AUTHORS:            Andreas Heumann, Mike Hesser
  7.  * BUGS:          none
  8.  * TO DO:         
  9.  * HISTORY:       DATE        NAME    COMMENT
  10.  *                        10.08.95    ah        first release
  11.  ***************/
  12.  
  13. #include <exec/memory.h>
  14. #include <rexx/storage.h>
  15. #include <rexx/rxslib.h>
  16. #include <exec/types.h>
  17.  
  18. #include <pragma/exec_lib.h>
  19. #include <pragma/rexxsyslib_lib.h>
  20.  
  21. #include <fstream.h>
  22. #include <stdlib.h>
  23. #include <stdio.h>
  24. #include <string.h>
  25. #include <time.h>
  26.  
  27. #define c_Radius        1
  28. #define g                15
  29.  
  30. struct Library *RexxSysBase = NULL;
  31. static struct MsgPort *HostPort;
  32. static struct MsgPort *ReplyPort = NULL;
  33. static int msgs = 0;        // remaining messages
  34.  
  35. #define RED        0
  36. #define GREEN    1
  37. #define BLUE    2
  38.  
  39. typedef struct tSphere
  40. {
  41.     float x,y,z;
  42.    float m;
  43.     float vx,vy,vz;
  44.     char color;
  45. };
  46.  
  47. struct tSphere *SpList = NULL;
  48.  
  49. /*************
  50.  * FUNCTION:        SendRexxMsg
  51.  * VERSION:            1.0 08.05.1995
  52.  * DESCRIPTION:    Sends a single command to Rexx host
  53.  * INPUT:            HostPort        pointer to host message port
  54.  *                        SingleMsg    message to send
  55.  * OUTPUT:            none
  56.  * HISTORY:       DATE        NAME        COMMENT
  57.  *                        08.05.95    ah            first release
  58.  *************/
  59. void SendRexxMsg(struct MsgPort *HostPort, char *SingleMsg)
  60. {
  61.     struct RexxMsg *RexxMessage;
  62.  
  63.     // free previous messages
  64.     while(RexxMessage = (struct RexxMsg*)GetMsg(ReplyPort))
  65.     {
  66.         // Remove Rexx message
  67.         ClearRexxMsg(RexxMessage,1);
  68.         DeleteRexxMsg(RexxMessage);
  69.         msgs--;
  70.     }
  71.  
  72.     // Valid pointers given?
  73.     if(HostPort && SingleMsg)
  74.     {
  75.         // Create a Rexx message
  76.         RexxMessage = (struct RexxMsg *)CreateRexxMsg(ReplyPort,"",NULL);
  77.         if(RexxMessage)
  78.         {
  79.             RexxMessage->rm_Args[0] = CreateArgstring(SingleMsg, strlen(SingleMsg));
  80.             RexxMessage->rm_Action = RXFF_RESULT;
  81.  
  82.             // Send packet
  83.             PutMsg(HostPort,(struct Message *)RexxMessage);
  84.             msgs++;
  85.         }
  86.     }
  87. }
  88.  
  89. /*************
  90.  * FUNCTION:        InitObjects
  91.  * VERSION:            1.0 08.05.1995
  92.  * DESCRIPTION:    Initalize sphere positions and attributes
  93.  * INPUT:            pics        amount of pics
  94.  *                        spheres    amount of spheres
  95.  * OUTPUT:            none
  96.  * HISTORY:       DATE        NAME        COMMENT
  97.  *                        08.05.95    ah            first release
  98.  *************/
  99. void InitObjects(int pics,int spheres)
  100. {
  101.     int i;
  102.  
  103.     for(i=0; i<spheres; i++)
  104.     {
  105.         SpList[i].x = float(rand())/RAND_MAX*50+35;
  106.         SpList[i].y = float(rand())/RAND_MAX*50+10;
  107.         SpList[i].z = -(float(rand())/RAND_MAX*50+150);
  108.         SpList[i].m = 1;
  109.         SpList[i].vx = 0;
  110.         SpList[i].vy = float(rand())/RAND_MAX*4-2;
  111.         SpList[i].vz = 40/pics;
  112.         if(rand()<RAND_MAX/3)
  113.         {
  114.             SpList[i].color = RED;
  115.         }
  116.         else
  117.         {
  118.             if(rand()<RAND_MAX/2)
  119.             {
  120.                 SpList[i].color = GREEN;
  121.             }
  122.             else
  123.             {
  124.                 SpList[i].color = BLUE;
  125.             }
  126.         }
  127.     }
  128. }
  129.  
  130. /*************
  131.  * FUNCTION:        MoveObjects
  132.  * VERSION:            1.0 08.05.1995
  133.  * DESCRIPTION:    Move spheres to new position
  134.  * INPUT:            pics        amount of pics
  135.  *                        spheres    amount of spheres
  136.  * OUTPUT:            none
  137.  * HISTORY:       DATE        NAME        COMMENT
  138.  *                        08.05.95    ah            first release
  139.  *************/
  140. void MoveObjects(int pics,int spheres)
  141. {
  142.     int i;
  143.     for(i=0; i<spheres; i++)
  144.     {
  145.         SpList[i].x += SpList[i].vx*(40/pics);
  146.         SpList[i].y += SpList[i].vy*(40/pics);
  147.         SpList[i].z += SpList[i].vz*(40/pics);
  148.         SpList[i].vy -= g*.1;
  149.         if(SpList[i].y <= c_Radius)
  150.         {
  151.             SpList[i].vy = -SpList[i].vy;
  152.         }
  153.     }
  154. }
  155.  
  156. /*************
  157.  * FUNCTION:        SaveObjects
  158.  * VERSION:            1.0 08.05.1995
  159.  * DESCRIPTION:    Send scene via ARexx to RayStorm 
  160.  * INPUT:            pic        number of picture
  161.  *                        spheres    amount of spheres
  162.  * OUTPUT:            none
  163.  * HISTORY:       DATE        NAME        COMMENT
  164.  *                        08.05.95    ah            first release
  165.  *************/
  166. void SaveObjects(int pic,int spheres)
  167. {
  168.     int i;
  169.     char buffer[256];
  170.  
  171.     sprintf(buffer, "SETCAMERA 90 35 0 %2.2f 35 %2.2f 0 1 0 56.25 45", SpList[0].x, SpList[0].z);
  172.     SendRexxMsg(HostPort, buffer);
  173.     SendRexxMsg(HostPort, "POINTLIGHT 150 400 50 255 255 255 SHADOW");
  174.     SendRexxMsg(HostPort, "NEWSURFACE CHECKER");
  175.     SendRexxMsg(HostPort, "REFLECT 50 50 50");
  176.     SendRexxMsg(HostPort, "DIFFUSE 50 50 200");
  177.     SendRexxMsg(HostPort, "AMBIENT 50 50 200");
  178. //    SendRexxMsg(HostPort, "IMTEXTURE '/textures/checker.itx 0 1 0  0 0 0  10 10 10 23 23 90");
  179.     SendRexxMsg(HostPort, "PLANE CHECKER");
  180.     SendRexxMsg(HostPort, "NEWSURFACE RED");
  181.     SendRexxMsg(HostPort, "DIFFUSE 255 0 0");
  182.     SendRexxMsg(HostPort, "AMBIENT 255 0 0");
  183.     SendRexxMsg(HostPort, "NEWSURFACE GREEN");
  184.     SendRexxMsg(HostPort, "DIFFUSE 0 255 0");
  185.     SendRexxMsg(HostPort, "AMBIENT 0 255 0");
  186.     SendRexxMsg(HostPort, "NEWSURFACE BLUE");
  187.     SendRexxMsg(HostPort, "DIFFUSE 0 0 255");
  188.     SendRexxMsg(HostPort, "AMBIENT 0 0 255");
  189.     for(i=0; i<spheres; i++)
  190.     {
  191.         switch(SpList[i].color)
  192.         {
  193.             case RED:
  194.                 sprintf(buffer, "SPHERE RED %2.2f %2.2f %2.2f 1", SpList[i].x,  SpList[i].y, SpList[i].z);
  195.                 SendRexxMsg(HostPort, buffer);
  196.                 break;
  197.             case GREEN:
  198.                 sprintf(buffer, "SPHERE GREEN %2.2f %2.2f %2.2f 1", SpList[i].x,  SpList[i].y, SpList[i].z);
  199.                 SendRexxMsg(HostPort, buffer);
  200.                 break;
  201.             case BLUE:
  202.                 sprintf(buffer, "SPHERE BLUE %2.2f %2.2f %2.2f 1", SpList[i].x,  SpList[i].y, SpList[i].z);
  203.                 SendRexxMsg(HostPort, buffer);
  204.                 break;
  205.         }
  206.     }
  207.     SendRexxMsg(HostPort, "STARTRENDER");
  208.     sprintf(buffer, "SAVEPIC pics/pic.%04d", pic);
  209.     SendRexxMsg(HostPort, buffer);
  210.     SendRexxMsg(HostPort, "CLEANUP");
  211. }
  212.  
  213. /*************
  214.  * FUNCTION:        cleanup
  215.  * VERSION:            1.0 08.05.1995
  216.  * DESCRIPTION:    close libs ...
  217.  * INPUT:            none
  218.  * OUTPUT:            none
  219.  * HISTORY:       DATE        NAME        COMMENT
  220.  *                        08.05.95    ah            first release
  221.  *************/
  222. void cleanup()
  223. {
  224.     struct RexxMsg *RexxMessage;
  225.  
  226.     if(ReplyPort)
  227.     {
  228.         // free messages
  229.         while(msgs)
  230.         {
  231.             // Wait for message
  232.             WaitPort(ReplyPort);
  233.             // get messages
  234.             while(RexxMessage = (struct RexxMsg*)GetMsg(ReplyPort))
  235.             {
  236.                 // Remove Rexx message
  237.                 ClearRexxMsg(RexxMessage,1);
  238.                 DeleteRexxMsg(RexxMessage);
  239.                 msgs--;
  240.             }
  241.         }
  242.         // Free reply port signal bit
  243.         FreeSignal(ReplyPort->mp_SigBit);
  244.         // Free the replyport itself
  245.         FreeMem(ReplyPort,sizeof(struct MsgPort));
  246.     }
  247.     if (RexxSysBase)
  248.         CloseLibrary(RexxSysBase);
  249.     if(SpList)
  250.         delete [ ] SpList;
  251.     exit(0);
  252. }
  253.  
  254. /*************
  255.  * FUNCTION:        Init
  256.  * VERSION:            1.0 08.05.1995
  257.  * DESCRIPTION:    Open libs, init ports
  258.  * INPUT:            spheres    amount of spheres
  259.  * OUTPUT:            none
  260.  * HISTORY:       DATE        NAME        COMMENT
  261.  *                        08.05.95    ah            first release
  262.  *************/
  263. void Init(int spheres)
  264. {
  265.     // is raystrom already active ?
  266.     HostPort = (struct MsgPort *)FindPort("RAYSTORM");
  267.     if(!HostPort)
  268.     {
  269.         cout << "Raystorm is not running please start it\n";
  270.         cleanup();
  271.     }
  272.     // Allocate a reply port
  273.     ReplyPort = (struct MsgPort *)AllocMem(sizeof(struct MsgPort),MEMF_PUBLIC | MEMF_CLEAR);
  274.     if(ReplyPort)
  275.     {
  276.         ReplyPort->mp_SigBit = AllocSignal(-1);
  277.         if(ReplyPort->mp_SigBit != -1)
  278.         {
  279.             ReplyPort->mp_Node.ln_Type    = NT_MSGPORT;
  280.             ReplyPort->mp_Flags            = PA_SIGNAL;
  281.             ReplyPort->mp_SigTask        = FindTask(NULL);
  282.  
  283.             ReplyPort->mp_MsgList.lh_Head = (struct Node *)&ReplyPort->mp_MsgList.lh_Tail;
  284.             ReplyPort->mp_MsgList.lh_Tail = 0;
  285.             ReplyPort->mp_MsgList.lh_TailPred = (struct Node *)&ReplyPort->mp_MsgList.lh_Head;
  286.         }
  287.     }
  288.  
  289.     // Open rexxsyslib library.
  290.     RexxSysBase = OpenLibrary("rexxsyslib.library",0L);
  291.     if (!RexxSysBase)
  292.     {
  293.         cout << "Can't open rexxsyslib.library\n";
  294.         cleanup();
  295.     }
  296.     SpList = new tSphere[spheres];
  297.     if(!SpList)
  298.     {
  299.         cout << "Not enough memory\n";
  300.         cleanup();
  301.     }
  302.     srand(time(0));
  303. }
  304.  
  305. void main(int argc, char *argv[])
  306. {
  307.     int i,pics;
  308.     int spheres = 10;
  309.  
  310.     // Parse arguments
  311.     if(argc != 2 && argc != 3)
  312.     {
  313.         cout << "Sphereanim V1.0 1995 by Andreas Heumann" << endl;
  314.         cout << "Usage: sphanim picnum [spheres]" << endl;
  315.         cout << "   picnum   - amount of pictures to create" << endl;
  316.         cout << "   spheres  - amount of spheres" << endl;
  317.         cleanup();
  318.     }
  319.     sscanf(argv[1],"%d",&pics);
  320.     if(pics<2)
  321.     {
  322.         cout << "I need at least two pictures to create." << endl;
  323.         cleanup();
  324.     }
  325.     if(argc == 3)
  326.     {
  327.         sscanf(argv[2],"%d",&spheres);
  328.         if(pics<1)
  329.         {
  330.             cout << "I need at least one sphere." << endl;
  331.             cleanup();
  332.         }
  333.     }
  334.  
  335.     Init(spheres);
  336.     InitObjects(pics,spheres);
  337.  
  338.     for(i=0; i<pics; i++)
  339.     {
  340.         SendRexxMsg(HostPort, "SETSCREEN 160 128");
  341.         SendRexxMsg(HostPort, "SETWORLD 0 0 0 0 0 0");
  342.         MoveObjects(pics,spheres);
  343.         SaveObjects(i+1,spheres);
  344.     }
  345.  
  346.     cleanup();
  347. }